#pragma once

#include <spdlog/spdlog.h>
#include "../ecs/ecs.hpp"
#include "./internal_message.h"

#include "qbe/src/undoRedo/undoRedo.hpp"

namespace qbe::msg {

enum struct RenderCode {
  RefUpdate = 1,
  RefCacheAdd,
  RefCacheUpdate,

  GeoCacheAdd,      // 添加临时绘制对象
  GeoCacheDrawing,  // 更新临时绘制对象
  GeoCacheUpdate,   // 更新临时绘制对象

  GeoAdd,     // 添加对象
  GeoUpdate,  // 更新对象
  GeoDelete,  // 删除对象

  CellUpdate,  // cell刷新
};

// 渲染消息
struct RenderEvent {
  RenderCode code;
};

struct TestEvent {
  int code{1};
};

//void ref_event_send_test(ecs::EventWriter<RefUpdateEvent> event);
void test_event_send(ecs::EventWriter<TestEvent> event);
void test_event_recv(ecs::EventReader<TestEvent> events);
void test_event_clear(ecs::Res<ecs::Event<TestEvent>> res);

// 清除所有消息
void clear_msgs(ecs::Query query);

class MessagePlugin : public ecs::Plugin {
 public:
  void build(ecs::App& app) {
    app.init_event<TestEvent>();

    // 注册引用更新消息
    app.init_event<RenderEvent>();

    app.add_systems<ecs::stage::Update>(test_event_send, test_event_recv, test_event_clear);
    //app.add_systems<ecs::stage::Update>(ref_event_send_test);
    app.add_systems<ecs::stage::Last>(clear_msgs);
  };
};

}  // namespace qbe::msg